import 'package:flutter/material.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter_spinkit/flutter_spinkit.dart';
import 'package:font_awesome_flutter/font_awesome_flutter.dart';
import 'package:open_file/open_file.dart';
import '../../helper/cacheHelper.dart';

import '../../helper/downloadHelper.dart';
import '../../helper/fileHelper.dart';
import '../../helper/iconHelper.dart';
import '../../helper/routeHelper.dart';
import '../../helper/signHelper.dart';
import '../../helper/uidata.dart';
import '../../model/signitem.dart';
import '../../model/space_item.dart';

import '../../spaces/filespace/fileSpacePanel.dart';
import '../../spaces/filespace/fileSpace_helper.dart';
import '../../spaces/filespace/file_view.dart';

import '../../widgets/inputWidget.dart';
import '../../widgets/signatureCard2.dart';
import '../../helper/Helper.dart';

import '../../helper/channelHelper.dart';

//import 'DocReview_authorty.dart';

import '../../model/doc_review.dart';
import 'docreview_huizhi.dart';

class DocReviewInfoPage extends StatefulWidget {
  @override
  DocReviewInfoPage(this.s1, this.callback);
  final DocReview s1;
  final RefrushCallback callback;

  @override
  createState() => new DocReviewInfoPageState();
}

class DocReviewInfoPageState extends State<DocReviewInfoPage> {
  late DocReview s1;
  //final TextEditingController c2 = new TextEditingController();
  late List<SignItem> qList;
  late List<SpaceItem> fList;

  @override
  void initState() {
    super.initState();
    //s1 = widget.s1;
    //UIData.CurrentUser!.Authority="水调值班员,电厂值班员";
    loadData();
  }

  loadData() async {
    var s2 = await DocReview.GetByID(widget.s1.ID);
    var qL = await SignItem.GetList(s2!.Guid);
    var list1 = await SpaceItem.GetList(s2!.Guid, 0, 1, 100);
    // for(var f1 in list1){
    //   f1.progress=await getProgress(f1);
    // }
    // print(s2.toJson());
    setState(() {
      s1 = s2;
      qList = qL;
      fList = list1;
    });
  }

  upload(String imgId) async {
    // await Helper.GetstoragePermission();
    if (imgId == null) return;
    var pb1 = await SignHelper.uploadQM2(s1, imgId, qList);
    print(pb1);
    await loadData();
    widget.callback();
    ChannelHelper.showToast("签字成功！");
  }

  List<Widget> getWidgetList3() {
    List<Widget> list1 = <Widget>[];
    if (qList.length == 0) return list1;
    for (var a in qList) {
      var u1 = CacheHelper.GetUser(a.UserId);
      list1.add(InputWidget.getQMTile2(a.Title + "-" + u1.Name, a.IfOK, a.QM,
          Helper.DateTimeFormat(a.QmTime)));
      list1.add(Divider(
        height: 0.0,
      ));
    }

    return list1;
  }

//  Future<int> getProgress(SpaceItem s1) async{
//        var fPath = await DownloadHelper.GetLocalFile(s1.Guid);
//                 if (fPath != "") return 100;
//                 return 0;
//  }

  List<Widget> getFileWidgetList() {
    List<Widget> list1 = <Widget>[];
    list1.add(ListTile(
      title: Text("发文附件"),
    ));

    list1.add(Divider(
      height: 0.0,
    ));

    for (var a in fList) {
      list1.add(FileSpaceHelper.fileCard3(context, a));
      list1.add(Divider(
        height: 0.0,
      ));
    }

    return list1;
  }

  List<Widget> getAllWidget(BuildContext context) {
    List<Widget> list1 = <Widget>[];
    List<Widget> list2 = <Widget>[];

    list2.add(InputWidget.getInfo("发文标题", s1.Title));
    list2.add(Divider(
      height: 0.0,
    ));
    list2.add(InputWidget.getInfo2("发文详情", s1.Content));
    list2.add(Divider(
      height: 0.0,
    ));

    list2.add(InputWidget.getUserTile(context, "创建者", s1.UserID));

    list2.add(Divider(
      height: 0.0,
    ));
    list2.add(InputWidget.getInfo("创建时间", Helper.DateTimeFormat(s1.CreateTM)));

    list1.add(InputWidget.inCard2(list2));

    // list1.add(InputWidget.inCard(FileSpacePanel("发文附件", s1.Guid)));
    list1.add(InputWidget.inCard2(getFileWidgetList()));

    list1.add(InputWidget.inCard2(getWidgetList3()));

    if (s1.State >= 3) {
      list1.add(InputWidget.inCard(FileSpacePanel("归档文件", s1.FileSpaceId)));
    }

    return list1;
  }

  void showPanel(BuildContext context) {
    showModalBottomSheet(
      context: context,
      builder: (BuildContext context) {
        return SignatureCard2Page(upload, SignHelper.getTitle(s1.State, qList));
      },
    );
  }

  Widget getFloatingButton(BuildContext context) {
    if (s1 == null) return new Container();
    if (SignHelper.ifQZ(s1.State, qList)) {
      return FloatingActionButton(
          child: Icon(FontAwesomeIcons.penAlt),
          onPressed: () {
            showPanel(context);
          });
    }

    if (s1.State >= 3 && Helper.StrPB(s1.UserID, UIData.CurrentUser!.Guid)) {
      return FloatingActionButton(
          child: Icon(FontAwesomeIcons.fileAlt),
          onPressed: () {
            RouteHelper.ShowPage(
                context, DocReviewHuiZhiPage(s1, widget.callback));
          });
    }

    return new Container();
  }

  @override
  Widget build(BuildContext context) {
    // if (s1 == null) return Center(child:SpinKitCircle(color: Colors.blue,));

    // TODO: implement build
    return Scaffold(
        appBar: AppBar(title: Text('发文审批')),
        //bottomSheet:bottomPanel2() ,
        floatingActionButton: getFloatingButton(context),
        body: s1 == null
            ? Center(
                child: SpinKitCircle(
                color: Colors.blue,
              ))
            : SingleChildScrollView(
                child: new Container(
                    margin: EdgeInsets.only(top: 8.0),
                    child: new Column(children: getAllWidget(context))),
              ));
  }
}
